package com.sun.weekend.ui.activity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;

import com.sun.weekend.R;
import com.sun.weekend.adapter.CommentAdapter;
import com.sun.weekend.beans.base.VideoBean;
import com.sun.weekend.util.LogUtils;
import com.sun.weekend.view.FixedListView;

import java.util.ArrayList;
import java.util.List;

public class DemoActivity extends AppCompatActivity implements AbsListView.OnScrollListener {

    FixedListView listview;
    CommentAdapter adapter;
    int pos=0;

    int w;
    int h;
    List<VideoBean> beans = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        w = getResources().getDisplayMetrics().widthPixels;
        h = getResources().getDisplayMetrics().heightPixels;
        listview = (FixedListView) findViewById(R.id.list);
        listview.setOnScrollListener(this);
        adapter = new CommentAdapter(this);
        listview.setAdapter(adapter);
        for (int i = 0; i < 10; i++) {
            VideoBean bean = new VideoBean();
            bean.setUrl("http://123.mp4");
            beans.add(bean);
        }
        adapter.append(beans);
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

            //设置全屏
            WindowManager.LayoutParams attr = getWindow().getAttributes();
            attr.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(attr);
            getSupportActionBar().hide();

            View view = listview.getChildAt(0);
            view.getLayoutParams().height = w;
            View v = view.findViewById(R.id.player);
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            params.leftMargin = 0;
            params.topMargin = 0;
            params.height = w;
            params.width = h;
            v.setLayoutParams(params);

        }else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){

            //取消全屏
            WindowManager.LayoutParams attr = getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attr);
            getSupportActionBar().show();

            View view = listview.getChildAt(0);
            view.getLayoutParams().height = 720;
            View v = view.findViewById(R.id.player);
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            params.leftMargin = 360;
            params.topMargin = 120;
            params.height = 600;
            v.setLayoutParams(params);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        LogUtils.logi(firstVisibleItem+" "+visibleItemCount+" "+totalItemCount);
    }
}
